package basics;

import Array;
import pixi.core.text.Text;
import framework.widgets.SpriteButton;
import pixi.core.textures.Texture;
import pixi.core.sprites.Sprite;


typedef Callback = Int->Void;
class ResultState extends GameState{

	private var _title:String;
	private var _msg:String;
	private var _callback:Callback;

	private var _options:Array<String>;

	public function new(tile:String, msg:String, options:Array<String>, cb:Callback) {
		super(0.4);
		_options = options;
		_title = tile;
		_msg = msg;
		_callback = cb;
    }

	/**
	*	添加选项
	**/
	public function addOption(opt:String){
		_options.push(opt);
	}

	override public function onEnter():Void {
		// 背景框
		var frame = new Sprite(Texture.fromImage("assets/textures/game/pop.png"));
		frame.anchor.set(0.5);
		frame.position.set(0, 0);
		this.addChild(frame);

		// 选项
		var n = _options.length;
		var w = 205; // 按钮宽度
		var space = 17; // 排列间隔
		var width = n * w + (n - 1) * space;
		var x = -(width/2 - w/2);
		for(i in 0...n){

			var btn = new SpriteButton(Texture.fromImage("assets/textures/game/button.png"));
			btn.anchor.set(0.5);
			btn.position.set(x + i * w + i * space, 158);
			frame.addChild(btn);

			btn.onClick = function(){
				this.onCloseClick(i);
			};

			var title = _options[i];
			var textOK = new Text(title, {
				fontSize: 30,
				fontFamily : 'Arial',
				fill : '#FFFFFF',
				align : 'center',
			});
			textOK.anchor.set(0.5);
			btn.addChild(textOK);
		}


		// 标题
		var textTitle = new Text(_title, {
			fontSize: 30,
			fontFamily : 'Arial',
			fill : '#FFFFFF',
			align : 'center',
		});
		textTitle.anchor.set(0.5);
		textTitle.position.set(0, -165);
		frame.addChild(textTitle);

		// 描述
		var textDesc = new Text(_msg, {
			fontSize: 30,
			fontFamily : 'Arial',
			fill : '#226ce8',
			align : 'center',
			breakWords: true,
			wordWrap: true,
			wordWrapWidth: 350
		});

		textDesc.anchor.set(0.5);
		textDesc.position.set(0, 0);
		frame.addChild(textDesc);

	}

	override public function onExit():Void {
	}

	private function onCloseClick(idx:Int){
		SceneManager.instance.popState();
		if(_callback != null){
			_callback(idx);
		}
	}
}
